package com.nus.iss.ad.dbhelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.nus.iss.ad.model.Employee;
import com.nus.iss.ad.model.Inventory;
import com.nus.iss.ad.model.Order;
import com.nus.iss.ad.model.Request;
import com.nus.iss.ad.model.RequestDetail;
import com.nus.iss.ad.utils.JsonParser;
import com.nus.iss.ad.utils.MyConfig;

public class RequestDBHelper {
	
	private class RejectMessage{
		private String rejectionMessage;
		public String getRejectionMessage() {
			return rejectionMessage;
		}
		public void setRejectionMessage(String rejectionMessage) {
			this.rejectionMessage = rejectionMessage;
		}
		public RejectMessage(){
			
		}
	}
	
	public void approveRequest(int requestId){
		String url=MyConfig.BACKEND_PATH + "/api/approveRequest"  ;
	     url=url+"/"+requestId;
	     
	     JsonParser.postStream(url, Integer.toString(requestId));
	}
	
	public void rejectRequest(int requestId,String rejectReason){
		String url=MyConfig.BACKEND_PATH + "/api/rejectRequest"  ;
	     url=url+"/"+requestId;
	     
	     RejectMessage rejectMessage=new RejectMessage();
	     rejectMessage.setRejectionMessage(rejectReason);
	     Gson gson=new Gson();
	     String jsonRejectMessage=gson.toJson(rejectMessage);
	     
	     JsonParser.postStream(url, jsonRejectMessage);
	}
	
	public List<Request> getPendingRequest(){
		List<Request> r=new ArrayList<Request>();
		 String url=MyConfig.BACKEND_PATH + "/api/getPendingRequest"  ;
	     JSONArray arr=JsonParser.getJSONArrayFromUrl(url);
	     try {
	     for(int i=0;i<arr.length();i++)
	     {
				JSONObject obj=arr.getJSONObject(i);
				Request req=new Request();
				
				int requestId=obj.getInt("requestId");
				req.setRequestId(requestId);
				
				String str=obj.getString("requestDate");
				str=str.substring(0, 10);
				Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(str);
				req.setRequestDate(date);
				
				//Read Employee
				JSONObject objEmployee=obj.getJSONObject("employee");
				String name=objEmployee.getString("name");
				Employee emp=new Employee();
				emp.setName(name);
				req.setEmployee(emp);
				
				//Read RequestDetails
				JSONArray arrRd=obj.getJSONArray("requestDetails");
				RequestDetail[] requestDetails=new RequestDetail[arrRd.length()];
				for(int rdi=0;rdi<arrRd.length();rdi++){
					JSONObject objRd=arrRd.getJSONObject(rdi);
					RequestDetail rd=new RequestDetail();
					rd.setQuantity(objRd.getInt("quantity"));
					rd.setInventoryId(objRd.getString("inventoryId"));
					
					JSONObject objInv=objRd.getJSONObject("inventory");
					Inventory inv=new Inventory();
					inv.setDescription(objInv.getString("description"));
					inv.setStock(objInv.getInt("stock"));
					
					rd.setInventory(inv);
					requestDetails[rdi]=rd;
				}
				req.setRequestDetails(requestDetails);
				r.add(req);
	     }
	     } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     catch(ParseException pe){
	    	 pe.printStackTrace();
	     }
		 return r;
	}
}
